package com.example.aqarapp.model;

import android.content.ComponentName;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;

import com.example.aqarapp.entity.BaseDeviceInfo;
import com.example.aqarapp.entity.BulbInfo;
import com.example.aqarapp.entity.CurtainInfo;
import com.example.aqarapp.entity.DeviceInfo;
import com.example.aqarapp.entity.FamilyInfo;
import com.example.aqarapp.entity.RoomInfo;
import com.example.aqarapp.entity.SocketInfo;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;



public class FamilyInfoModel {

//    private TemplateAidlInterface mService = mTemplateService;


    private List<BaseDeviceInfo> quickDeviceList = new ArrayList<>(); //存快捷控制所有设备信息
    private ArrayList<BaseDeviceInfo> curPlaceDeviceList = new ArrayList<>();

    private JSONObject firstLayerObject;
    private JSONObject secondLayerObject;
    private JSONObject thirdLayerObject;
    // private List<FamilyInfo> familyInfoList = new ArrayList<>();
//    private List<RoomInfo> roomInfoList = new ArrayList<>();
//    private List<DeviceInfo> deviceInfoList = new ArrayList<>();
    private String firstLayerdata;
    private String secondLayerdata;
    private JSONArray jsonArray;
    private String familyName;
    private String familyId;
    private JSONArray familyjsonArray;
    private JSONArray roomjsonArray;
    private JSONArray devicejsonArray;

    private String deviceModel;
    private String did;
    private String deviceState;
    private String deviceName;
    private JSONArray bulbColorjsonArray;
    private JSONArray bulbLightjsonArray;
    private JSONArray bulbStatejsonArray;
    private String bulbColorValue;
    private String bulbStateValue;
    private String bulbLightValue;
    private String curtainStateValue;
    private String socketStateValue;
    private BaseDeviceInfo bulbInfo;
    private BaseDeviceInfo curtainInfo;
    private BaseDeviceInfo socketInfo;
    private DeviceInfo deviceInfo;
    private RoomInfo roomInfo;
    private FamilyInfo familyInfo;
    private JSONArray bulbjsonArray;
    private String roomName;
    private String roomId;

    public FamilyInfoModel() {
        Log.d("liyao", "FamilyInfoModel");
    }

    public List<FamilyInfo> getFamilyInfo() {
        List<FamilyInfo> familyInfoList = null;
        try {
            //遍历每个账号所有的family信息
            // String allFamilyInfo = mTemplateService.getAllFamilyInfos("");
//            familyInfoList.clear();
            familyjsonArray = getFamilyjsonArray(allFamilyInfo);
            familyInfoList = new ArrayList<>();
            //遍历每个family所有的room信息
            for (int i = 0; i < familyjsonArray.length(); i++) {
//                roomInfoList.clear();
                JSONObject thirdLayerObject = familyjsonArray.getJSONObject(i);
                familyName = thirdLayerObject.optString("positionName")+"_"+i;
                familyId = thirdLayerObject.optString("positionId")+"_"+i;
                //String roomInfo = mTemplateService.getFamilyRooms(familyId);
                roomjsonArray = getFamilyjsonArray(SingleFamilyRoomInfo);
                //遍历每个Room所有的Device信息
                List<RoomInfo> roomInfoList =  new ArrayList<>();
                for (int j = 0; j < roomjsonArray.length(); j++) {
//                    deviceInfoList.clear();
                    thirdLayerObject = roomjsonArray.getJSONObject(j);
                    roomName = thirdLayerObject.getString("positionName")+"_"+i+"_"+j;
                    roomId = thirdLayerObject.getString("positionId")+"_"+i+"_"+j;
                    // String DeviceInfo = mTemplateService.getRoomDevices(roomId);
                    devicejsonArray = getFamilyjsonArray(SingleRoomDeivcesInfo);
                    List<DeviceInfo> deviceInfoList = new ArrayList<>();
                    for (int k = 0; k < devicejsonArray.length(); k++) {
                        thirdLayerObject = devicejsonArray.getJSONObject(k);
                        deviceModel = thirdLayerObject.getString("model");
                        did = thirdLayerObject.getString("did")+"_"+i+"_"+j+"_"+k;
                        deviceState = thirdLayerObject.getString("state"); //
                        deviceName = thirdLayerObject.getString("deviceName")+"_"+i+"_"+j+"_"+k; //
                        //通过model创建对应的对象并写入属性
                        //灯泡
                        if (deviceModel.equals("lumi.light.aqcn02")) {
//                            String bulbColorInfo = mTemplateService.getDeviceResourceValue("14.2.85",did); //灯泡色温
//                            String bulbStateInfo = mTemplateService.getDeviceResourceValue("14.2.85",did);//灯泡状态
//                            String bulbLighrInfo = mTemplateService.getDeviceResourceValue("14.2.85",did);//灯泡亮度
                            bulbColorValue = getDeviceAttributeInfo(bulbColorInfo);
                            bulbStateValue = getDeviceAttributeInfo(bulbStateInfo);
                            bulbLightValue = getDeviceAttributeInfo(bulbLightInfo);
                            bulbInfo = new BulbInfo(roomName, bulbLightValue, bulbStateValue, bulbColorValue);
                            deviceInfo = new DeviceInfo(deviceName, did, deviceModel, deviceState, bulbInfo);
                            deviceInfoList.add(deviceInfo);
                        }//窗帘
                        else if (deviceModel.equals("lumi.curtain.v1")) {
                            //  String curtainStateInfo = mTemplateService.getDeviceResourceValue("14.2.85",did);//窗帘状态
                            curtainStateValue = getDeviceAttributeInfo(curtainStateInfo);
                            curtainInfo = new CurtainInfo(roomName, curtainStateValue);
                            deviceInfo = new DeviceInfo(deviceName, did, deviceModel, deviceState, curtainInfo);
                            deviceInfoList.add(deviceInfo);
                        }//开关
                        else if (deviceModel.equals("lumi.plug.v1")) {
                            socketStateValue = getDeviceAttributeInfo(socketStateInfo);
                            socketInfo = new SocketInfo(roomName, socketStateValue);
                            deviceInfo = new DeviceInfo(deviceName, did, deviceModel, deviceState, socketInfo);
                            deviceInfoList.add(deviceInfo);
                        }
                    }
                    roomInfo = new RoomInfo(roomId, roomName, deviceInfoList);
                    roomInfoList.add(roomInfo);
                }
                familyInfo = new FamilyInfo(familyId, familyName, roomInfoList);
                familyInfoList.add(familyInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return familyInfoList;
    }

//   // public List<RoomInfo> getRoomInfo() {
//        return roomInfoList;
//    }

    //获取设备属性信息 方法封装
    public  String getDeviceAttributeInfo(String attr) throws JSONException {
        firstLayerObject = new JSONObject(attr);
        firstLayerdata = firstLayerObject.get("result").toString();
        bulbjsonArray=new JSONArray(firstLayerdata);
        thirdLayerObject=bulbjsonArray.getJSONObject(0);
        return thirdLayerObject.getString("value");
    }

    //获取JsonArray 方法封装
    public JSONArray getFamilyjsonArray(String info) throws JSONException {
        firstLayerObject = new JSONObject(info);
        firstLayerdata = firstLayerObject.get("result").toString();
        secondLayerObject = new JSONObject(firstLayerdata);
        secondLayerdata = secondLayerObject.get("data").toString();
        return  new JSONArray(secondLayerdata);
    }

    ////获取房间名称
    //TODO 这里第二个for循环是因为"快捷"需要所有的数据，后面可以直接根据解析的数据添加list
    public List<String> getRoomName() {
//        for (int i = 0; i < roomInfoList.size(); i++) {
//            for (Iterator iter = roomInfoList.get(i).getRoomDeviceInfoMap().entrySet().iterator(); iter.hasNext(); ) {
//                Map.Entry element = (Map.Entry) iter.next();
//                roomNameList.add((String) element.getKey());
//            }
//        }
        return  new ArrayList<String>();
    }

    //获取快捷location下所有设备信息
    public List<BaseDeviceInfo> getQuickDeviceItem() {
        return quickDeviceList;
    }

    //获取当前location下所有设备信息
    public ArrayList<BaseDeviceInfo> getCurPlaceDeviceList(String location) {
//        curPlaceDeviceList.clear();
//        for(int i =0 ;i <quickDeviceList.size();i++){
//            if(location.equals(quickDeviceList.get(i).location)){
//                curPlaceDeviceList.add(quickDeviceList.get(i));
//            }
//        }
        return curPlaceDeviceList;
    }




    //===========================================


    String socketStateInfo = "{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": [\n" +
            "           {\n" +
            "              \"timeStamp\": 1617886614312,\n" +
            "              \"resourceId\": \"4.1.85\",\n" +
            "              \"value\": \"0\",\n" +
            "              \"subjectId\": \"virtual2.11774113824794\"\n" +
            "            }\n" +
            "        ]\n" +
            "}";

    String curtainStateInfo = "{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": [\n" +
            "           {\n" +
            "              \"timeStamp\": 1617886614312,\n" +
            "              \"resourceId\": \"4.1.85\",\n" +
            "              \"value\": \"1\",\n" +
            "              \"subjectId\": \"virtual2.11774113824794\"\n" +
            "            }\n" +
            "        ]\n" +
            "}";

    String bulbColorInfo ="{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": [\n" +
            "            {\n" +
            "              \"timeStamp\": 1617886614176,\n" +
            "              \"resourceId\": \"14.2.85\",\n" +
            "              \"value\": \"200\",\n" +
            "              \"subjectId\": \"virtual2.11774113824794\"\n" +
            "            }\n" +
            "        ]\n" +
            "}";

    String bulbStateInfo ="{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": [\n" +
            "           {\n" +
            "              \"timeStamp\": 1617886614312,\n" +
            "              \"resourceId\": \"4.1.85\",\n" +
            "              \"value\": \"1\",\n" +
            "              \"subjectId\": \"virtual2.11774113824794\"\n" +
            "            }\n" +
            "        ]\n" +
            "}";

    String bulbLightInfo ="{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "     \"result\": [\n" +
            "           {\n" +
            "              \"timeStamp\": 1617886614312,\n" +
            "              \"resourceId\": \"14.1.85\",\n" +
            "              \"value\": \"90\",\n" +
            "              \"subjectId\": \"virtual2.11774113824794\"\n" +
            "            }\n" +
            "        ]\n" +
            "}";

    String SingleRoomDeivcesInfo = "{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": {\n" +
            "        \"data\": [\n" +
            "            {\n" +
            "               \"parentDid\": \"\",\n" +
            "               \"createTime\": 1615899097028,\n" +
            "               \"timeZone\": \"GMT+09:00\",\n" +
            "               \"model\": \"lumi.light.aqcn02\",\n" +
            "               \"updateTime\": 1615899097028,\n" +
            "               \"modelType\": 1,\n" +
            "               \"state\": 1,\n" +
            "               \"did\": \"virtual2.07737309957642\",\n" +
            "               \"deviceName\": \"灯泡\"\n" +
            "            },\n" +
            "            {\n" +
            "               \"parentDid\": \"\",\n" +
            "               \"createTime\": 1615901365498,\n" +
            "               \"timeZone\": \"GMT+09:00\",\n" +
            "               \"model\": \"lumi.curtain.v1\",\n" +
            "               \"updateTime\": 1615899097028,\n" +
            "               \"modelType\": 1,\n" +
            "               \"state\": 1,\n" +
            "               \"did\": \"virtual2.07639066475218\",\n" +
            "               \"deviceName\": \"窗帘\"\n" +
            "            },\n" +
            "            {\n" +
            "               \"parentDid\": \"\",\n" +
            "               \"createTime\": 1615901365498,\n" +
            "               \"timeZone\": \"GMT+09:00\",\n" +
            "               \"model\": \"lumi.plug.v1\",\n" +
            "               \"updateTime\": 1615899097028,\n" +
            "               \"modelType\": 1,\n" +
            "               \"state\": 0,\n" +
            "               \"did\": \"virtual2.07639066475218\",\n" +
            "               \"deviceName\": \"插座\"\n" +
            "            }\n" +
            "        ],\n" +
            "        \"totalCount\": 2\n" +
            "    }\n" +
            "}";




    String allFamilyInfo = "{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": {\n" +
            "        \"data\": [\n" +
            "            {\n" +
            "                \"positionName\": \"星河湾\",\n" +
            "                \"positionId\": \"real3.5654656566\",\n" +
            "                \"createTime\": 1603337732360,\n" +
            "                \"description\": \"\",\n" +
            "                \"parentPositionId\": \"real1.768799734012641280\"\n" +
            "            },\n" +
            "            {\n" +
            "                \"positionName\": \"汤臣一品\",\n" +
            "                \"positionId\": \"real3.12321321312323\",\n" +
            "                \"createTime\": 1603337732360,\n" +
            "                \"description\": \"\",\n" +
            "                \"parentPositionId\": \"real1.768799734012641280\"\n" +
            "            }\n" +
            "        ],\n" +
            "        \"totalCount\": 1\n" +
            "    }\n" +
            "}";

    String SingleFamilyRoomInfo = "{\n" +
            "    \"code\": 0,\n" +
            "    \"message\": \"Success\",\n" +
            "    \"msgDetails\": \"\",\n" +
            "    \"requestId\": \"\",\n" +
            "    \"result\": {\n" +
            "        \"data\": [\n" +
            "            {\n" +
            "                \"positionName\": \"客厅\",\n" +
            "                \"positionId\": \"real2.768799734306242560\",\n" +
            "                \"createTime\": 1603337732360,\n" +
            "                \"description\": \"\",\n" +
            "                \"parentPositionId\": \"real1.768799734012641280\"\n" +
            "           },\n" +
            "            {\n" +
            "                \"positionName\": \"卧室\",\n" +
            "                \"positionId\": \"real2.7687997343123233311\",\n" +
            "                \"createTime\": 1603337732360,\n" +
            "                \"description\": \"\",\n" +
            "                \"parentPositionId\": \"real1.768799734012641280\"\n" +
            "            },\n" +
            "            {\n" +
            "               \"positionName\": \"阳台\",\n" +
            "                \"positionId\": \"real2.7687997343123233311\",\n" +
            "                \"createTime\": 1603337732360,\n" +
            "                \"description\": \"\",\n" +
            "                \"parentPositionId\": \"real1.768799734012641280\"\n" +
            "              }\n" +
            "        ],\n" +
            "        \"totalCount\": 1\n" +
            "    }\n" +
            "}";


}
